--require "MainScene"

JLSJLayer={}
JLSJLayerFunc={}
JLSJLayerFunc.__index = JLSJLayerFunc
setmetatable(JLSJLayer, JLSJLayerFunc)

JLSJLayer.NodeID = {HeroTable=1001}
local HeroItemID = {ImgBK=2001, TongShuai=2002, ZhanShu=2003, BaoPo=2004, JueZhao=2005, Name=2006, Button=3001}

JLSJLayerFunc.getNode = function(self, gid)
  local l = {}
  setmetatable(l, JLSJLayerFunc)
  l.node = CCBReader:load("JunXiaoScene2.ccbi")
  print("JLSJLayer.getNode", JLSJLayer.node)
  l:init(gid) 
  return l 
end

JLSJLayerFunc.getScene = function(self)
  local sc = CCScene:create()
  sc:addChild(JLSJLayer:getNode().node)  
  print("JLSJLayer.getScene", sc)
  return sc
end

JLSJLayerFunc.init = function(self, gid)
  --self:initTable()
  self.gid = gid
  setObjectExtTable(self.node:getChildByTag(1020), self)
  setObjectExtTable(self.node:getChildByTag(1021), self)
  setObjectExtTable(self.node:getChildByTag(1022), self)
  setObjectExtTable(self.node:getChildByTag(1023), self)
  setObjectExtTable(self.node:getChildByTag(1024), self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
 
  print("JLSJLayerFunc.init", gid) 
  local general = General:create(gid)
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(string.format("使用目标: %s", general.generalConfig.name))
  
  local bl = TypeCast(self.node:getChildByTag(1020), "CCButton")
  local br = TypeCast(self.node:getChildByTag(1021), "CCButton")
  self.uiStepper = UIStepper:create(bl, br, "onUIStepperChanged", self)
  local item = Item:createWithId(30)--经验球
  local count = 1
  if item.itemInfo~=nil then
    count = item.itemInfo.n+1
  end
  self.uiStepper:setCount(count)
  self.uiStepper:setIndex(0)
end
JLSJLayerFunc.onUIStepperChanged = function(self, index)
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d/%d", index, self.uiStepper.count-1))
end
JLSJLayerFunc.onUseButton = function(self, node, event)
  NetworkRequest.usePill(self.gid, self.uiStepper.index, "onUsePillComplete", self)
  LoadScene:showLoadingScene()
end
JLSJLayerFunc.onUsePillComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self.uiStepper:setIndex(0)
    self:onCloseButton()
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end
JLSJLayerFunc.onMaxButton = function(self, node, event)
  local item = Item:createWithId(30)--经验球
  self.uiStepper:setIndex(item.itemInfo.n)
end
JLSJLayerFunc.onBuyButton = function(self, node, event)
  JLSJLayer.oldGid = self.gid
  MainScene.scene:removeChild(self.node, true)
  MainScene.scene:addChild(JLSJBuyPointLayer:getNode().node) 
end
JLSJLayerFunc.onCloseButton = function(self, node, event)
  MainScene.scene:removeChild(self.node, true)
  local l = JunXiaoScene:getNode()
  l.radioMenu:setMenuState(3)
  print("JLSJLayerFunc.onCloseButton", JLAllLayer.oldIndex)
  JLAllLayer.instance.uiStepper:setIndex(JLAllLayer.oldIndex)
  MainScene.scene:addChild(l.node)
end
